/**	
*	Drawing an open book : 
* second version : 
*- there is a problem between -PI/4 an -Pi/2 : trying to solve this one
*- on doit simuler la tranche
*/

import ij.ImagePlus;
import ij.plugin.PlugIn;
import ij.process.ColorProcessor;
import ij.process.ImageProcessor;

import java.awt.Color;
import java.awt.Polygon;

public class EnhancedBook_ extends ImagePlus implements PlugIn {

	

	public void run(String arg) {
		ImageProcessor primeIP = new ColorProcessor(2000, 1200);
		int R=700; 
		int r=20; 
		int xa=1000-200,xb=1000-500,ya=1000-500,yb=1000-200;
		ya=ya+300; 
		yb=yb+300; 
		xa=xa+300; 
		xb=xb+300; 
		//int xa=1500,xb=500,ya=1500,yb=1800; 
		int abscisses[]={xa,xb,xb,xa}; 
		int ordonnees[]={ya,yb,yb-R,ya-R};
		
		primeIP.setLineWidth(3);
		
		Polygon background=new Polygon(new int[]{0,0,2000,2000},new int[]{0,2000,2000,0},4);
		primeIP.setColor(new Color(0.98f,0.98f,0.98f)); 
		primeIP.fillPolygon(background);
		
		double alpha=Math.PI/2; 
		int pas=50; 
		double pasAlpha=(Math.PI+0.1)/(1.34*pas);
		float red=0.1f; 
		float green=0f;
		float blue=0f; 
		float pasColor=(float)0.7/pas; 
		
		abscisses[0]=(int)(xa-r*Math.sin(alpha));
		abscisses[1]=(int)(xb-r*Math.sin(alpha));
		abscisses[2]=(int)(xb-R*Math.sin(alpha)); 
		abscisses[3]=(int)(xa-R*Math.sin(alpha)); 
		
		ordonnees[0]=(int)(ya-r*Math.cos(alpha));
		ordonnees[1]=(int)(yb-r*Math.cos(alpha));		
		ordonnees[2]=(int)(yb-R*Math.cos(alpha)); 
		ordonnees[3]=(int)(ya-R*Math.cos(alpha));
		
		Polygon pl=new Polygon(abscisses,ordonnees,4);
		primeIP.setColor(new Color(red,green,blue)); 
		primeIP.drawPolygon(pl); 
		primeIP.setColor(new Color(red,0,0)); 
		primeIP.fillPolygon(pl);
		
		
		for(int i=0;i<pas-2;i++){
		pl=new Polygon(abscisses,ordonnees,4);
		primeIP.setColor(new Color(red,green,blue)); 
		primeIP.drawPolygon(pl); 
		primeIP.setColor(new Color(red,0,0)); 
		primeIP.fillPolygon(pl);
			
		alpha=alpha-pasAlpha;
		abscisses[0]=(int)(xa-r*Math.sin(alpha));
		abscisses[1]=(int)(xb-r*Math.sin(alpha));
		abscisses[2]=(int)(xb-R*Math.sin(alpha)); 
		abscisses[3]=(int)(xa-R*Math.sin(alpha)); 
		
		ordonnees[0]=(int)(ya-r*Math.cos(alpha));
		ordonnees[1]=(int)(yb-r*Math.cos(alpha));		
		ordonnees[2]=(int)(yb-R*Math.cos(alpha)); 
		ordonnees[3]=(int)(ya-R*Math.cos(alpha));
		red+=pasColor; 
		green+=pasColor;
		blue+=pasColor;			
		}
	
//		 Les derniers 60 degres
		System.out.println(red+" "+green+" "+blue);
		
		red=0.9f; 
		blue=0.9f;
		green=0.9f;
		
		
		
		alpha=-(Math.PI/2); 
		abscisses[0]=(int)(xa-r*Math.sin(alpha));
		abscisses[1]=(int)(xb-r*Math.sin(alpha));
		abscisses[2]=(int)(xb-R*Math.sin(alpha)); 
		abscisses[3]=(int)(xa-R*Math.sin(alpha));
		
		ordonnees[0]=(int)(ya-r*Math.cos(alpha));
		ordonnees[1]=(int)(yb-r*Math.cos(alpha));	
		ordonnees[2]=(int)(yb-R*Math.cos(alpha)); 
		ordonnees[3]=(int)(ya-R*Math.cos(alpha));
		
		pas=pas/3; 
		pasAlpha=-Math.PI/(3*pas);
		pasColor=(float)0.33/pas;
		System.out.println(pasColor);
		float pasColorRed=(float)0.22/pas; 
		for (int i=0;i<pas-2;i++){
			pl=new Polygon(abscisses,ordonnees,4);
			primeIP.setColor(new Color(red,green,blue)); 
			
			primeIP.drawPolygon(pl); 
			primeIP.setColor(new Color(red,0,0)); 
			primeIP.fillPolygon(pl);
			
			alpha=alpha-pasAlpha;
			abscisses[0]=(int)(xa-r*Math.sin(alpha));
			abscisses[1]=(int)(xb-r*Math.sin(alpha));
			abscisses[2]=(int)(xb-R*Math.sin(alpha)); 
			abscisses[3]=(int)(xa-R*Math.sin(alpha)); 
			
			ordonnees[0]=(int)(ya-r*Math.cos(alpha));
			ordonnees[1]=(int)(yb-r*Math.cos(alpha));	
			ordonnees[2]=(int)(yb-R*Math.cos(alpha)); 
			ordonnees[3]=(int)(ya-R*Math.cos(alpha));
			red-=pasColorRed; 
			green-=pasColor;
			blue-=pasColor;
		}
		
		// Premiere de couverture
		pl=new Polygon(new int[]{xb-r,xb-R,xb-R,xb-r},new int[]{yb+20,yb+20,yb,yb},4);
		primeIP.setColor(Color.BLUE); 
		primeIP.fillPolygon(pl);
		
		
		// La derniere de couverture
		
		pl=new Polygon(new int[]{xb+r,xb+r,xb+R,xb+R},new int[]{yb+20,yb,yb,yb+20},4);
		primeIP.setColor(Color.BLUE); 
		primeIP.fillPolygon(pl);
		pl=new Polygon(new int[]{xb+R,xa+R,xa+R,xb+R},new int[]{yb,ya,ya+20,yb+20},4);
		primeIP.setColor(Color.BLUE); 
		primeIP.fillPolygon(pl);
		
		setProcessor("Enhanced Book", primeIP);
		show();
	}

	

}
